package com.qi.demo01.Demo;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author zjq
 * @date 2020/3/27 15:21
 */
//循环打印ABCD
public class PrintABCD {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(1);
        Semaphore semaphore1 = new Semaphore(1);
        Semaphore semaphore2 = new Semaphore(1);
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    //System.out.println(Thread.currentThread().getName());
                    System.out.print("A");
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                }

            }).start();
        }
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    semaphore1.acquire();
                    //System.out.println(Thread.currentThread().getName());
                    System.out.print("B");
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore1.release();
                }

            }).start();
        }
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    semaphore2.acquire();
                    //System.out.println(Thread.currentThread().getName());
                    System.out.print("C");
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore2.release();
                }

            }).start();
        }

    }
}
